package com.pj.project.mqtt.myserver;

import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.pj.project.mqtt.util.Tools.bytesToHex;

@Service
public class MainService {
    private static final Logger logger = LoggerFactory.getLogger(MainService.class);

    @Autowired
    private MqttClientTemplate client;

    public boolean publish(String topic, byte[] message) {
        logger.info("发送给:"+ topic +"消息："+ bytesToHex(message));
        client.publish(topic, ByteBuffer.wrap(message));
        return true;
    }




}
